package Dao.Imp;

import Dao.UserApplicationDao;
import modal.UserApplication;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by tan on 2017/8/24.
 * ${time}
 */
@Repository
@Transactional
public class UserApplicationDaoImp implements UserApplicationDao {
    @Autowired
    SessionFactory sessionFactory;


    public String allowApplication(UserApplication application) {
        Session session =sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("update user_application set result = 1 WHERE username =?");
            Query query = session.createSQLQuery(sql.toString()).setString(0,application.getUsername());
            if(query.executeUpdate()==1)
            {
                return "success";
            }

        }catch (Exception e)
        {
            System.err.println("Dao.Imp.UserApplicationDaoImp\n申请审核失败");
            e.printStackTrace();
        }
        return "error";
    }

    public String refuseApplication(UserApplication application) {

        Session session =sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("delete from user_application  WHERE username =?");
            Query query = session.createSQLQuery(sql.toString()).setString(0,application.getUsername());
            if(query.executeUpdate()==1)
            {
                return "success";
            }

        }catch (Exception e)
        {
            System.err.println("Dao.Imp.UserApplicationDaoImp\n申请删除失败");
            e.printStackTrace();
        }
        return "error";
    }

    public List<UserApplication> getAllApplication() {
        Session session = sessionFactory.getCurrentSession();
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("select * from user_application where result='0' " );
            Query query =session.createSQLQuery(sql.toString());
            query.setResultTransformer(Transformers.aliasToBean(UserApplication.class));
            return query.list();
        }catch (Exception e)
        {
            System.out.println("Dao.Imp.UserApplicationDaoImp\n获取申请列表失败");
            e.printStackTrace();
        }
        return null;
    }

    public UserApplication getOneApplication(String username) {
        Session session =sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from user_application where username =?");
            Query query =session.createSQLQuery(sql.toString()).setString(0,username);
            query.setResultTransformer(Transformers.aliasToBean(UserApplication.class));
            return (UserApplication) query.uniqueResult();
        }
        catch (Exception e)
        {
            System.out.println("Dao.Imp.UserApplicationDaoImp\n获取申请实例失败");
            e.printStackTrace();
        }
        return null;
    }

}
